﻿using EmitMapper;
using STHPMS1.BLL.B_Core;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using STHPMS1.ICore.I_Serv;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace STHPMS1.BLL.B_Bus
{
    public class B_SturctureBus : BllBase, IB_SturctureBill
    {
        public void Save<T>(T entity)
            where T : class ,IB_Sturcture
        {
            if (entity.Rdwayid == null) throw new Exception("路线编号不能为空!");
            if (entity.Struname == null) throw new Exception("构造物名称不能为空!");
            if (entity.Sttype_Dictid == 0) throw new Exception("结构类型不能为空!");
            if (entity.Endmp <= entity.Beginmp) throw new Exception("终点桩号要大于起点桩号!");
            if (this.Scene.Dal.Sthpms1.Exists<B_Roadway>(p => (entity.Beginmp<p.Beginmp || entity.Endmp>p.Endmp) &&
                                                               p.Rdwayid==entity.Rdwayid))
            {
                throw new Exception("桩号不在该路线的桩号范围!");
            }
            if (this.Scene.Dal.Sthpms1.Exists<B_Sturcture>(p => (p.Rdwayid == entity.Rdwayid) &&
                                                                      (p.Beginmp < entity.Beginmp &&
                                                                       p.Endmp > entity.Beginmp) && 
                                                                       (p.Direction_Dictid==entity.Direction_Dictid) && 
                                                                       p.Struid!=entity.Struid))
            {
                throw new Exception("起点桩号在路线中某个构造物中已经存在!");
            }
            if (this.Scene.Dal.Sthpms1.Exists<B_Sturcture>(p => (p.Rdwayid == entity.Rdwayid) &&
                                                                         (p.Beginmp < entity.Endmp &&
                                                                          p.Endmp > entity.Endmp) &&
                                                                         (p.Direction_Dictid == entity.Direction_Dictid) && 
                                                                          p.Struid!=entity.Struid))
            {
                throw new Exception("终点桩号在路线中某个构造物中已经存在!");
            }
            if (this.Scene.Dal.Sthpms1.Exists<B_Sturcture>(p => (p.Rdwayid == entity.Rdwayid) &&
                                                                        (p.Beginmp > entity.Beginmp &&
                                                                         p.Endmp < entity.Endmp) &&
                                                                       (p.Direction_Dictid == entity.Direction_Dictid) && 
                                                                        p.Struid!=entity.Struid))
            {
                throw new Exception("此桩号在路线中已经存在构造物!");
            }

            if (string.IsNullOrEmpty(entity.Struid))
            {
                entity.Struid = MyApp.Locator.GetInstance<IKeyGenrateService>().NewSturctureId(entity);
            }

            entity.His_Version = this.Scene.Bll.B_Roadway.GetNewHistoryVersion(entity.Rdwayid);
            entity.Importdate = DateTime.Now;
            this.Scene.Dal.Sthpms1.Save(entity);            
        }

        public void Remove(IB_Sturcture entity)
        {
            B_Sturcture_History history = ObjectMapperManager.DefaultInstance.GetMapper<IB_Sturcture, B_Sturcture_History>().Map(entity);
            if (history != null)
            {
                history.Importdate = DateTime.Now;
                this.Scene.Dal.Sthpms1.Save(history);
            }
            this.Scene.Dal.Sthpms1.DeleteById<B_Sturcture>(entity.Struid);
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IB_Sturcture
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info, k => k.Importdate, true);
        }
    }
}
